package com.drojian.root;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.drojian.demo1.BaseFragment1;
import com.drojian.demo.R;
import com.drojian.demo2.BaseFragment2;
import com.drojian.demo3.BaseFragment3;
import com.drojian.page4.BaseFragment4;
import com.drojian.page5.BaseFragment5;
import com.drojian.root.database.SqlUser;
import com.drojian.side.BackgroundMusicService;
import com.drojian.side.InformationActivity;
import com.drojian.side.service.NotificationServiceMusic;
import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager2 root_viewpager;
    private Button btn_one,btn_two,btn_three,btn_four,btn_five;

    private MyFragmentVpAdapter myFragmentVpAdapter;
    private List<Fragment> mFragmentList;

    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private Cursor user;
    private ImageView image ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        initView();
        initImage();
        initData();
        setupViewPager();
    }



    private void initData() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(BaseFragment1.newInstance());
        mFragmentList.add(BaseFragment2.newInstance());
        mFragmentList.add(BaseFragment3.newInstance());
        mFragmentList.add(BaseFragment4.newInstance());
        mFragmentList.add(BaseFragment5.newInstance());
    }
    // 设置适配器 及 滑动监听
    private void setupViewPager() {
        myFragmentVpAdapter = new MyFragmentVpAdapter(getSupportFragmentManager(), getLifecycle());
        myFragmentVpAdapter.setFragments(mFragmentList);
        root_viewpager.setAdapter(myFragmentVpAdapter);
        root_viewpager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                onViewPager2Select(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
        // 默认进入首页
        onViewPager2Select(0);


        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);

    }

    // 设置当滑动时的样式
    private void onViewPager2Select(int position) {
        resetBottomStyle();
        switch (position){
            case 0:
                btn_one.setBackgroundColor(getResources().getColor(R.color.accent_sky_blue));
                break;
            case 1:
                btn_two.setBackgroundColor(getResources().getColor(R.color.accent_sky_blue));
                break;
            case 2:
                btn_three.setBackgroundColor(getResources().getColor(R.color.accent_sky_blue));
                break;
            case 3:
                btn_four.setBackgroundColor(getResources().getColor(R.color.accent_sky_blue));
                break;
            case 4:
                btn_five.setBackgroundColor(getResources().getColor(R.color.accent_sky_blue));
                break;
            default:
                break;
        }

    }
    // 把其他底部恢复到原样式
    private void resetBottomStyle() {
        btn_one.setBackground(getResources().getDrawable(R.drawable.edit_text_bg_blue));
        btn_two.setBackground(getResources().getDrawable(R.drawable.edit_text_bg_blue));
        btn_three.setBackground(getResources().getDrawable(R.drawable.edit_text_bg_blue));
        btn_four.setBackground(getResources().getDrawable(R.drawable.edit_text_bg_blue));
        btn_five.setBackground(getResources().getDrawable(R.drawable.edit_text_bg_blue));


    }

    private void initView() {
        root_viewpager = findViewById(R.id.root_viewpager);
        btn_one = findViewById(R.id.btn_one);
        btn_two = findViewById(R.id.btn_two);
        btn_three = findViewById(R.id.btn_three);
        btn_four = findViewById(R.id.btn_four);
        btn_five = findViewById(R.id.btn_five);
        mNavigationView = findViewById(R.id.nv_draw);
        image = mNavigationView.getHeaderView(0).findViewById(R.id.image);
        mDrawerLayout = findViewById(R.id.drawer_layout);
        mNavigationView.setOnClickListener(this);
        Toolbar toolbar = findViewById(R.id.tool_bar);
        ActionBarDrawerToggle barDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(barDrawerToggle);
        barDrawerToggle.syncState();

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.drawer_first:
                        mDrawerLayout.close();
                        Intent intent1 = new Intent(Intent.ACTION_VIEW);
                        intent1.setData(Uri.parse("https://www.baidu.com"));
                        startActivity(intent1);
                        break;
                    case R.id.drawer_two:
                        Intent intent = new Intent(MainActivity.this, BackgroundMusicService.class);
                        intent.putExtra("action","root");
                        startService(intent);
                        mDrawerLayout.close();
                        break;
                    case R.id.drawer_btn_three:
                        root_viewpager.setCurrentItem(2);
                        mDrawerLayout.close();
                        break;
                    case R.id.drawer_four:
                        Intent notification = new Intent(MainActivity.this, NotificationServiceMusic.class);
                        notification.putExtra("action","notification");
                        startService(notification);
                        mDrawerLayout.close();
                        break;
                    case R.id.drawer_five:
                        mDrawerLayout.close();
                        String username = getIntent().getStringExtra("username");
                        Intent intent5 = new Intent(MainActivity.this, InformationActivity.class);
                        intent5.putExtra("username",username);
                        startActivity(intent5);
                        break;
                    case R.id.drawer_six:
                        mDrawerLayout.close();
                        finish();
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_one:
                root_viewpager.setCurrentItem(0);
                break;
            case R.id.btn_two:
                root_viewpager.setCurrentItem(1);
                break;
            case R.id.btn_three:
                root_viewpager.setCurrentItem(2);
                break;
            case R.id.btn_four:
                root_viewpager.setCurrentItem(3);
                break;
            case R.id.btn_five:
                root_viewpager.setCurrentItem(4);
                break;


        }
    }
    private void initImage() {
        SqlUser user1 = new SqlUser(this, "user.db", null, 1);
        SQLiteDatabase db = user1.getWritableDatabase();
        String username1 = getIntent().getStringExtra("username");
        user = db.query("user", null, "username = ?", new String[]{username1}, null, null, null);
        if (user != null && user.moveToFirst()) {
            // 获取图片字节数组并显示在ImageView上
            int imageIndex = user.getColumnIndex("image");
            byte[] imageBytes = user.getBlob(imageIndex);
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (image != null) {
                // 设置图片
                image.setImageBitmap(bitmap);
            } else {
                Toast.makeText(this,"有错误",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this,"有错误",Toast.LENGTH_SHORT).show();
        }
        if (user != null && !user.isClosed()) {
            user.close();
        }

    }

    @Override
    protected void onDestroy() {
        Intent intent = new Intent(this, BackgroundMusicService.class);
        stopService(intent);
        super.onDestroy();

    }
}
